package com.songyang.yougnsql.core.table;

import com.songyang.yougnsql.core.data.DataManager;
import com.songyang.yougnsql.core.parser.statement.*;
import com.songyang.yougnsql.core.table.res.BeginRes;
import com.songyang.yougnsql.core.utils.ByteUtils;
import com.songyang.yougnsql.core.version.VersionManager;

import java.io.File;

public interface TableManager {
    BeginRes begin(Begin begin);

    String commit(long xid) throws Exception;

    String abort(long xid);

    String show(long xid);

    String create(long xid, Create create) throws Exception;

    String insert(long xid, Insert insert) throws Exception;

    String read(long xid, Select select) throws Exception;

    String update(long xid, Update update) throws Exception;

    String delete(long xid, Delete delete) throws Exception;

    public static TableManager create(String path, VersionManager vm, DataManager dm) {
        BootStrap bootStrap = BootStrap.create(path);
        bootStrap.update(ByteUtils.long2Byte(0));
        return new TableManagerImpl(vm, dm, bootStrap);
    }

    public static TableManager open(String path, VersionManager vm, DataManager dm) {
        return new File(path + File.separator + BootStrap.BOOTSTRAP_FILE).exists() ?
                new TableManagerImpl(vm, dm, BootStrap.open(path)) : create(path, vm, dm);

    }
}
